package com.css.dzj.dzjdzqs.modules.apinfo.service.impl;

import com.alibaba.fastjson.JSON;
import com.css.dzj.dzjdzqs.common.util.HttpClientUtil;
import com.css.dzj.dzjdzqs.common.util.UUidUtil;
import com.css.dzj.dzjdzqs.modules.apinfo.entity.ApInfoEntity;
import com.css.dzj.dzjdzqs.modules.apinfo.mapper.ApInfoMapper;
import com.css.dzj.dzjdzqs.modules.apinfo.service.ApInfoService;
import com.css.dzj.dzjdzqs.modules.apinfo.vo.ApInfoVo;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *逻辑层
 * @Author WT
 * @Date 2020/6/29
 */
@Service
public class ApInfoServiceImpl implements ApInfoService {

    @Autowired
    JPAQueryFactory jpaQueryFactory;
    @Autowired
    private ApInfoMapper apInfoMapper;
    @Value("${AP_URL}")
    private String apUrl;

    @Override
    @Transactional
    public void add(List<ApInfoVo> apInfoVoList) {

        try {
            for(ApInfoVo apInfoVo :apInfoVoList){
                ApInfoEntity apInfoEntity = new ApInfoEntity();
                apInfoEntity.setId(apInfoVo.getId());
                apInfoEntity.setRecordUniqueIdentity(UUidUtil.getUuid());
                apInfoEntity.setName(apInfoVo.getExecutorName());
                apInfoEntity.setFullNameOfTheSubject(apInfoVo.getExecutorOrganization());
                apInfoEntity.setIdNumber(apInfoVo.getExecutorIdCard());
                apInfoEntity.setSex(apInfoVo.getExecutorSex());
                apInfoEntity.setBirthDate(apInfoVo.getExecutorBirthday());
                apInfoEntity.setPoliticalStatus(apInfoVo.getExecutorPoliticStatus());
                apInfoEntity.setRank(apInfoVo.getExecutorLevel());
                apInfoEntity.setNation(apInfoVo.getExecutorNationality());
                apInfoEntity.setHighestEducation(apInfoVo.getExecutorEducation());
                apInfoEntity.setLawStaffType(apInfoVo.getExecutorCharacter());
                apInfoEntity.setSupervisionStaff(apInfoVo.getExecutorSuperviseStatus());
                apInfoEntity.setQualificationLegalPro(apInfoVo.getExecutorLegalQualification());
                apInfoEntity.setContactTel(apInfoVo.getExecutorPhoneNum());
                apInfoEntity.setCode(apInfoVo.getExecutorCode());
                apInfoEntity.setAcademicQualifications(apInfoVo.getExecutorMajor());
                apInfoEntity.setLicenceIssuingAuthority(apInfoVo.getExecutorCertifyingAuthority());
                apInfoEntity.setEnforcementArea(apInfoVo.getExecutorArea());
                apInfoEntity.setEnforcementType(apInfoVo.getExecutorCategory());
                apInfoEntity.setValidityDocuments(apInfoVo.getExecutorCardInDate());
                apInfoMapper.save(apInfoEntity);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    @Transactional
    public void pushtInfo() {

        try{
            String msg = HttpClientUtil.doGet(apUrl+"/executorInfo/getApList");
            List<ApInfoVo> apInfoVoList = JSON.parseArray(msg, ApInfoVo.class);
            if(null!=apInfoVoList && apInfoVoList.size()>0){
                add(apInfoVoList);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
